/**
 * 
 */
var stations = null;
$(function() {
	//获取所有车站信息
	$.ajax({
		//https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.9056   在线版,可能不是最新版
		url : "assets/stations.json", //离线版
		type : "GET",
		success : function(data) {
			stations = data;
			initTrainDate();
		},
		async : false
	});


	/**
	 * 绑定搜索按钮方法
	 */
	$("#searchTrainBtn").click(function() {
		var trainDate = $("#trainDate").val().trim();
		var fromStation = stationNameToCode($("#fromStation").val().trim());
		var toStation = stationNameToCode($("#toStation").val().trim());
		if (fromStation == "" || toStation == "") {
			alert("请输入正确的车站!");
			return null;
		}
		if (trainDate == "") {
			alert("请输入正确的日期!");
			return null;
		}

		$.ajax({
			//成人票：ADULT 学生票：0X00
			url : "ticket/getTrainList",
			type : "GET",
			//async : false,
			data : {
				"trainDate" : trainDate,
				"fromStation" : fromStation,
				"toStation" : toStation
			},
			success : function(result) {
				//			var result = $.parseJSON(re);
				if (result.result > 0) {
					var data = result.data;
					$("#trainRow").empty();
					$.each(data, function(i, trainInfo) {
						$("#trainRow").append('<tr id="tr' + trainInfo.trainCode + '">');
						$("#trainRow").append('<td class="selectTd" style="text-align:center;"><input type="checkbox" data-trainCode="' + trainInfo.trainCode + '" class="trainCheckBox"></td>');
						$("#trainRow").append('<td class="trainCode" style="width:100px;">' + trainInfo.trainCode + '</td>');
						$("#trainRow").append('<td class="trainDateTd" style="width:100px;">' + trainInfo.trainDate + '</td>');
						$("#trainRow").append('<td class="fromStationTd" style="width:100px;">' + trainInfo.fromStationName + '</td>');
						$("#trainRow").append('<td class="toStationTd" style="width:100px;">' + trainInfo.toStationName + '</td>');
						$("#trainRow").append('<td class="startTimeTd" style="width:100px;">' + trainInfo.startTime + '</td>');
						$("#trainRow").append('<td class="endTimeTd" style="width:100px;">' + trainInfo.endTime + '</td>');
						$("#trainRow").append('<td class="totalTimeTd" style="width:100px;">' + trainInfo.totalTime + '分钟</td>');
						$("#trainRow").append('<td class="hasTicketTd" data-hasTicket=' + trainInfo.hasTicket + ' style="width:100px;">' + (trainInfo.hasTicket ? "是" : "否") + '</td>');
						$("#trainRow").append("</tr>");
					});
				} else {
					$("#trainRow").empty();
					alert("没有查询到车次!");
				}
			},
			error : function(result) {
				console.log("awdawdawdawdawd");
			}
		});
	});

	$("#addRemindTaskBtn").click(function() {
		if ($(".trainCheckBox:checked").length == 0) {
			alert("请先选择车次!");
		}
		var phoneNum = $("#phoneNumInput").val().trim();
		if (!(/\d{11}/.test(phoneNum))) {
			alert("请输入手机号码!");
		}
		//trainCheckBox  data-trainCode
		var trainDate = $("#trainDate").val().trim();
		var trainCodeStr = "";
		$(".trainCheckBox").each(function(index, obj) {
			if ($(this).is(":checked")) {
				var trainCode = $(this).attr("data-trainCode");
				trainCodeStr += trainCode + ",";
			}
		});
		$.ajax({
			url : "ticket/addTask",
			type : "POST",
			data : {
				"trainDate" : trainDate,
				"trainCode" : trainCodeStr,
				"phoneNum" : phoneNum
			},
			success : function(data) {
				var result = $.parseJSON(data);
				if (result.result > 0) {
					alert("设置成功!");
				}
			},
			error : function() {}
		});

	});



});
//https://kyfw.12306.cn/otn/czxx/queryByTrainNo?train_no=4d000K10060C&from_station_telecode=ZKN&to_station_telecode=GZQ&depart_date=2018-06-22'
//列车 https://kyfw.12306.cn/otn/resources/js/query/train_list.js?scriptVersion=1.0




/***************/
function initTrainDate() {
	var ddd = new Date();
	var day = ddd.getDate();

	if (ddd.getMonth() < 10) {
		var month = "0" + (ddd.getMonth() + 1);
	}

	if (ddd.getDate() < 10) {
		day = "0" + ddd.getDate();
	}

	var datew = ddd.getFullYear() + "-" + month + "-" + day;
	var datew = datew.toString();

	$("#trainDate").val(datew);
}

function isBeforeToday(date) {
	var today = new Date();
	var year = today.getYear() - 1 + 1 + 1900;
	var month = today.getMonth() - 1 + 2;
	var day = today.getDate() - 1 + 2;


}

function prefixZero(num) {
	if (num < 10) {
		return "0" + num;
	} else {
		return "" + num;
	}

}


function getArchiveDay(value1, value2) {
	value1 = value1.replace(":", "");
	value2 = value2.replace(":", "");
	hour_value = Number(value1.substring(0, 2)) + Number(value2.substring(0, 2));
	min_value = Number(value1.substring(2, 4)) + Number(value2.substring(2, 4));
	if (min_value >= 60) {
		hour_value = hour_value + 1;
	}
	if (hour_value >= 24 && hour_value < 48) {
		return "次日";
	} else if (hour_value >= 48 && hour_value < 72) {
		return "两日";
	} else if (hour_value >= 72) {
		return "三日";
	} else {
		return "当日";
	}
}


//异步问题!!!!!!!!!!!!!!
function stationNameToCode(name) {
	for (key in stations) {
		if (stations[key] == name) {
			return key;
		}
	}
	return "";
}

function stationCodeToName(code) {
	for (key in stations) {
		if (key == code) {
			return stations[key];
		}
	}
	return "";
}